package game.Sprites;

import game.Configuration;
import game.Gamemode.GamemodeManager;
import game.Gamemode.StandardManager;
import game.Gamemode.TeamDeathmatchManager;
import game.Player.Player;
import game.Scene;
import game.Utils.GraphicsUtils;
import game.Vector3;
import game.Weapons.Weapon_info;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;

import java.awt.geom.Rectangle2D;
import java.util.List;

public class DroppedBomb extends Sprite {
    double size = 1.0;
    Image droppedSprite;

    public DroppedBomb(Vector3 position, double rotation, Color color) throws Exception {
        super(position, rotation, color);
        this.setZ(0.0);
        droppedSprite = SpriteResources.getPlantedBombImage();
    }


    @Override
    public void draw(GraphicsContext gd, Vector3 screenPos, double squareRes) {
        // Draw shadow
        GraphicsUtils.drawSpriteShadow(gd, screenPos, 1.0, 1.0, squareRes);

        // Draw bomb sprite
        GraphicsUtils.drawRotatedImage(gd, droppedSprite, rotation + Math.PI / 2,
                (screenPos.x - squareRes * size / 2),
                (screenPos.y - squareRes * size / 2),
                squareRes * size, squareRes * size);
    }

    @Override
    public Rectangle2D.Double getBound() {
        return new Rectangle2D.Double(this.position.x - size / 2, this.position.y - size / 2, size, size);
    }

    @Override
    public void update(double deltaTime) {
        // Comprobar para cada uno de los jugadores si alguno quiere coger el arma
        List<Player> playerList = TeamDeathmatchManager.getInstance().getPlayerList();
        for (int i = 0; i < playerList.size(); i++) {
            Player pl = playerList.get(i);
            if (!pl.isAlive())
                continue;

            Vector3 posPlayer = pl.getPosition();
            posPlayer.z = 0.0;

            Vector3 posBomb = this.getPosition();
            posBomb.z = 0.0;

            double distance2d = Vector3.distance(posPlayer, posBomb);

            if (distance2d < 0.4 && pl.getTeam() == ((StandardManager)GamemodeManager.getInstance()).getTerroristTeam()) {
                pl.setHasBomb(true);
                ((StandardManager)GamemodeManager.getInstance()).pickBomb();
                this.destroy();
                return;
            }
        }
    }
}
